package com.ganbo.thread.producer_consumer.single;

/**
 * 生产者
 *
 * @Author: ganbo
 * @Date: 2020/5/20 16:38
 */
public class Producer extends Thread {

    private Object lock;

    public Producer(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                if (ValueObject.value != null) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    String value = System.currentTimeMillis() + "";
                    ValueObject.value = value;
                    lock.notifyAll();
                    System.out.println(Thread.currentThread().getName() + "设置的值是:" + value);

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
